package com.woniuxy.store.app.apiImpl;

import cn.hutool.Hutool;
import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.food.client.foodskuclient.FoodSkuManageClient;
import com.woniuxy.food.client.foodskuclient.FoodSkuQueryClient;
import com.woniuxy.food.client.foodskupicclient.FoodSkuPicManageClient;
import com.woniuxy.food.client.foodskupicclient.FoodSkuPicQueryClient;
import com.woniuxy.food.client.param.InsertFoodSkuParam;
import com.woniuxy.food.client.param.SearchFoodSkuByConditionParam;
import com.woniuxy.food.client.param.UpdateFoodSkuParam;
import com.woniuxy.member.client.MemberQueryClient;
import com.woniuxy.member.client.result.ExchangeFoodClientDto;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.FoodSkuClient;
import com.woniuxy.store.client.param.foodParam.InsertFoodSkuParams;
import com.woniuxy.store.client.param.foodParam.SearchFoodSkuByConditionParams;
import com.woniuxy.store.client.param.foodParam.UpdateFoodSkuParams;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
@RequestMapping("/store/food")
public class FoodSkuClientImpl implements FoodSkuClient {

    @Autowired
    private FoodSkuQueryClient foodSkuQueryClient;

    @Autowired
    private FoodSkuManageClient foodSkuManageClient;

    @Autowired
    private FoodSkuPicQueryClient foodSkuPicQueryClient;

    @Autowired
    private FoodSkuPicManageClient foodSkuPicManageClient;
    @Autowired
    private MemberQueryClient memberQueryClient;

    @Override
    @Log("添加sku")
    public Result insertFoodSku(@Valid InsertFoodSkuParams var1) {
        InsertFoodSkuParam param = BeanUtil.toBean(var1, InsertFoodSkuParam.class);
        param.setStoreId(StoreIdUtil.getFromToken());
        return foodSkuManageClient.insertFoodSku(param);
    }

    @Override
    @Log("sku上下架")
    public Result updateFoodSkuStateByIds(Integer[] var1, String var2) {

        return foodSkuManageClient.updateFoodSkuStateByIds(StoreIdUtil.getFromToken(),var1,var2);
    }

    @Override
    @Log("修改sku信息")
    public Result updateFoodSku(@Valid UpdateFoodSkuParams var1) {
        UpdateFoodSkuParam param = BeanUtil.toBean(var1, UpdateFoodSkuParam.class);
        param.setStoreId(StoreIdUtil.getFromToken());
        return foodSkuManageClient.updateFoodSku(param);
    }

    @Override
    @Log("修改sku销量")
    public Result updateFoodSkuSalesCount(Integer var1, Long var2) {
        return foodSkuManageClient.updateFoodSkuSalesCount(StoreIdUtil.getFromToken(),var1,var2);
    }

    @Override
    @Log("修改sku折扣价")
    public Result updateFoodSkuDiscountPrice(Integer var1, BigDecimal var2) {
        return foodSkuManageClient.updateFoodSkuDiscountPrice(StoreIdUtil.getFromToken(),var1,var2);
    }

    @Override
    @Log("删除sku")
    public Result deleteFoodSkuByIds(Integer[] var1) {
        for (Integer skuId : var1) {
            Result<ExchangeFoodClientDto> result = memberQueryClient.getMemberFoodBySkuId(skuId);
            if (result.getData()!=null) {
                throw new MallException("当前菜品还在兑换列表中", CommonResultCodeEnum.INVALID_PARAM);
            }
        }
        return foodSkuManageClient.deleteFoodSkuByIds(StoreIdUtil.getFromToken(),var1);
    }

    @Override
    @Log("根据条件查询sku")
    public Result searchFoodSkuByCondition(@Valid SearchFoodSkuByConditionParams var1) {
        SearchFoodSkuByConditionParam param = BeanUtil.toBean(var1, SearchFoodSkuByConditionParam.class);
        param.setStoreId(StoreIdUtil.getFromToken());
        return foodSkuQueryClient.searchFoodSkuByCondition(param);
    }

    @Override
    @Log("根据快速查询码查询sku")
    public Result searchFoodSkuByQQC(Integer[] var1) {
        return foodSkuQueryClient.searchFoodSkuByQQC(var1);
    }

    @Override
    @Log("根据skuId查询sku")
    public Result searchFoodSkuById(Integer var1) {
        return foodSkuQueryClient.searchFoodSkuById(var1);
    }

    @Override
    @Log("从图片库添加sku额外图片")
    public Result insertFoodSkuPic(Integer var1, Integer[] var2) {
        return foodSkuPicManageClient.insertFoodSkuPic(StoreIdUtil.getFromToken(),var1,var2);
    }

    @Override
    @Log("查询sku额外图片")
    public Result searchFoodSkuPic(Integer var1) {
        return foodSkuPicQueryClient.searchFoodSkuPic(var1);
    }
}
